package main

import (
	"fmt"
	"go_test/interfaceTest/fake"
	"go_test/interfaceTest/real"
)

const url = "http//:www.imooc.com"
type Retriever interface {
	Get(string2 string) string
}

func download(r Retriever) string {
	return r.Get(url)
}

func inspect(r Retriever)  {
	fmt.Printf("%T %v\n", r, r)
	switch v := r.(type) {  //判断值类型
	case *fake.Retriever:
		fmt.Println("Contents: ", v.Contents)
	case *real.Retriever:
		fmt.Println("UserAgent:", v.UserAgent)
	}
}

//接口组合
type Poster interface {
	Post(url string, from map[string]string)string
}

func post(p Poster) string {
	//p.Post(url,     //调用post方法没有作用
	//	map[string]string{
	//	"name":"ye",
	//	})
	return "aaa"
}

type RetrieverPoster interface {
	Retriever
	Poster
}

func session(p RetrieverPoster) string{
	p.Post(url, map[string]string{
		"name" : "ye",
	})
	return p.Get(url)
}

func main()  {
	var p Poster
	fmt.Println(post(p))
	//var r Retriever
	retriever := &fake.Retriever{Contents: "www.fake baidu.com"}
	//r = retriever
	//inspect(r)
	//r = &real.Retriever{
	//	UserAgent: "Mozilla/5.0",
	//	TimeOut: time.Minute,
	//}
	//inspect(r)
	//fmt.Println(download(r))
	//fmt.Println(download(fake.Retriever{Contents: "www.fake baidu.com"}))

	//type assertion
	//realRetriever := r.(*real.Retriever)  //用.()来取具体的类型
	//fmt.Println(realRetriever.TimeOut)

	fmt.Println(session(retriever)) //组合接口调用
}
